<template>
  <el-dialog v-drag width="45%" :title="!dataForm.wareId ? '新增仓库' : '修改仓库'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="80px">
      <el-form-item label="仓库名称" prop="wareName">
        <el-input size="mini" v-model="dataForm.wareName" placeholder="仓库名称"></el-input>
      </el-form-item>
      <el-form-item label="仓库编号" prop="wareCode">
        <el-input type="number" size="mini" v-model="dataForm.wareCode" placeholder="仓库编号"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <quill-editor ref="remark" v-model="dataForm.remark" class="editor" :options="editorOption" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">取消</el-button>
      <el-button size="mini" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          wareId: undefined,
          wareName: '',
          wareCode: '',
          remark: ''
        },
        dataRule: {
          wareName: [{
            required: true,
            message: '仓库名称不能为空',
            trigger: 'blur'
          }],
          wareCode: [{
            required: true,
            message: '仓库编号不能为空',
            trigger: 'blur'
          }],
        },
        editorOption:{
           placeholder: '请输入......'
        }
      }
    },
    methods: {
      init(wareId) {
        this.dataForm.wareId = wareId
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.wareId) {
            this.$http({
              url: this.$http.adornUrl(`/goods/goodsware/info/${this.dataForm.wareId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.dataForm = data.goodsWare
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/goods/goodsware/${!this.dataForm.wareId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData(this.dataForm)
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
